from playwright import sync_playwright


def run(page):
    # Click text="採購管理"
    page.click("text=\"採購管理\"")

    # Click //li[normalize-space(.)='付款單管理' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/payment"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='付款單管理' and normalize-space(@role)='menuitem']")

    # Click //li[normalize-space(.)='採購單管理' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/purchase"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='採購單管理' and normalize-space(@role)='menuitem']")

    # Click //li[normalize-space(.)='採購' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/purchaseManage"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='採購' and normalize-space(@role)='menuitem']")

    # Click //li[normalize-space(.)='修改採購價' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/purchaseEditUnitPrice"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='修改採購價' and normalize-space(@role)='menuitem']")

    # Click text="CG2012210004"
    page.click("text=\"CG2012210004\"")


    # Click text="更多"
    page.click("text=\"更多\"")

    page.click(
        "//div[starts-with(normalize-space(.), '採購單號採購單狀態SKU產品名稱規格型號單位創建人創建時間預計採購數量採購人採購時間實際採購數量採購單價採購總價供應商测试供应商厦门波山食品有限公司厦门市源源生') and normalize-space(@aria-label)='dialog' and normalize-space(@role)='dialog']/div[1]/button[normalize-space(@aria-label)='Close']/i")



with sync_playwright() as playwright:
    browser = playwright.chromium.launch(headless=False)
    context = browser.newContext()

    # Open new page
    page = context.newPage()

    # Go to http://192.168.2.20:5400/#/login
    page.goto("http://192.168.2.20:5400/#/login")

    # Click input[placeholder="請輸入登錄賬號"]
    page.click("input[placeholder=\"請輸入登錄賬號\"]")

    # Fill input[placeholder="請輸入登錄賬號"]
    page.fill("input[placeholder=\"請輸入登錄賬號\"]", "zhanghuan")

    # Press Tab
    page.press("input[placeholder=\"請輸入登錄賬號\"]", "Tab")

    # Press Tab
    page.press("input[placeholder=\"請輸入登錄密碼\"]", "Tab")

    # Press Tab
    page.press("//button[normalize-space(.)='登錄']", "Tab")

    # Click input[placeholder="請輸入登錄密碼"]
    page.click("input[placeholder=\"請輸入登錄密碼\"]")

    # Fill input[placeholder="請輸入登錄密碼"]
    page.fill("input[placeholder=\"請輸入登錄密碼\"]", "123456")

    # Click //button[normalize-space(.)='登錄']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/dashboard"):
    with page.expect_navigation():
        page.click("//button[normalize-space(.)='登錄']")

    # Click span[role="button"]
    page.click("span[role=\"button\"]")

    # Click text=/.*深圳仓.*/
    page.click("text=/.*深圳仓.*/")
    for i in range(100):
        run(page)
